package com.tlx.app.geekbang;

import java.io.File;
import java.util.Arrays;
import java.util.List;
import java.util.Set;

import org.apache.commons.io.FilenameUtils;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.util.ResourceUtils;

import com.tlx.core.util.TFileUtils;
import com.tlx.core.util.TSpring;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.resource.ResourceUtil;
import cn.hutool.core.lang.Console;
import cn.hutool.core.util.StrUtil;

@SpringBootApplication
@ComponentScan(basePackages = { "com.tlx" })
public class GeekBangHtml {

    static String CMD = "D:\\work\\wkhtmltox\\bin\\wkhtmltopdf --page-width 180mm --page-height 1200mm -B 0 -L 0 -R 0 -T 0 \"{0}\" \"{1}\"";

    public static void main(String[] args) throws Exception {
        SpringApplication.run(GeekBangHtml.class, args);

        String DIR_BASE = "D:/0/";

        String titleStart = "持续" + "*";

        // 生成html
        genHtml(DIR_BASE, titleStart);

        // 生成html2pdf的命令
        genCmd(DIR_BASE);
    }

    public static void genHtml(String baseDir, String titleStart) throws Exception {
        String htmlDir = FileUtil.getAbsolutePath(baseDir + "/html");
        if (FileUtil.exist(htmlDir)) {
            throw new RuntimeException("html目录存在：" + htmlDir);
        }

        File assetsDirFile = ResourceUtils.getFile("classpath:geekbang/assets");
        FileUtil.copy(assetsDirFile, new File(htmlDir), true);

        String templateContent = ResourceUtil.getResourceObj("classpath:geekbang/assets/geekbang_template.html").readUtf8Str();
        StringRedisTemplate srt = TSpring.getBean(StringRedisTemplate.class);
        Set<String> keySet = srt.keys(titleStart);
        for (String key : keySet) {
            String content = srt.opsForValue().get(key);
            content = StrUtil.replace(templateContent, "{{content}}", content);

            String fileName = key + ".html";
            fileName = StrUtil.replace(fileName, "|", "_");
            fileName = StrUtil.replace(fileName, " ", "");
            fileName = StrUtil.replace(fileName, "?", "？");

            FileUtil.writeUtf8String(content, new File(htmlDir, fileName));
        }
    }

    public static void genCmd(String baseDir) {
        String htmlDir = FileUtil.getAbsolutePath(baseDir + "/html");
        String pdfDir = FileUtil.getAbsolutePath(baseDir + "/pdf");
        if (FileUtil.exist(pdfDir)) {
            throw new RuntimeException("pdf目录存在：" + pdfDir);
        }
        String batFile = new File(pdfDir, "genPdf.bat").getAbsolutePath();

        FileUtil.del(batFile);
        FileUtil.mkdir(pdfDir);

        List<String> htmlList = TFileUtils.getFileListByExt(htmlDir, "html");

        for (String htmlPath : htmlList) {
            String pdfPath = new File(pdfDir, FilenameUtils.getBaseName(htmlPath) + ".pdf").getAbsolutePath();
            String cmd = StrUtil.indexedFormat(CMD, htmlPath, pdfPath);
            FileUtil.appendLines(Arrays.asList(cmd), batFile, "gbk");
            Console.log(cmd);
        }

    }

}
